package samples.online;

import java.net.URL;

import de.fmui.spheon.jsoap.Envelope;
import de.fmui.spheon.jsoap.Fault;
import de.fmui.spheon.jsoap.ParamBag;
import de.fmui.spheon.jsoap.RPCCall;
import de.fmui.spheon.jsoap.SoapConfig;
import de.fmui.spheon.jsoap.transport.http.SoapHttpClient;

/**
 * This class is a client of a webserivce of Spehon JSOAP (Version >0.1.4). 
 * (See http://spheon-jsoap.sourceforge.net/webservices.php)
 * It returns 'true' if a given integer is a prime number, 'false' otherwise.
 *
 * To run this client be sure that you have an XML-Parser (e.g. Xerces), spheon-jsoap.jar 
 * and this class in your classpath. 
 */
public class isPrimeNumberClient
{
	private int number;
	private URL proxy;
	
	public isPrimeNumberClient(int number, URL proxy)
	{
		this.number = number;
		this.proxy = proxy;
				
		doIt();
	}
	
	public void doIt()
	{
		long startTime;
		long processTime;
		long endTime;		
		
		// get configuration
		SoapConfig sc = new SoapConfig();
		
		// prepare a call
		RPCCall call = new RPCCall(sc, "urn:SpheonJSOAPStatistics", "isPrimeNumber");

		try {
			
			call.setParameter("number", new Integer(number), int.class);
				
			// execute call (via HTTP)
		    SoapHttpClient shc = new SoapHttpClient(sc, proxy);
		    startTime = System.currentTimeMillis();			    	
		    Envelope answer = shc.invokeHTTP(new URL("http://soap.fmui.de/RPC"), "", call.getEnvelope()); 
			processTime = System.currentTimeMillis();
		
			// check for a fault message
			if(answer.hasFault()) {			
				Fault f = answer.getFault();
				System.out.println("Faultcode:   " + f.getFaultcode());
				System.out.println("Faultstring: " + f.getFaultstring());
				System.out.println("Faultactor:  " + f.getFaultactor());		
				return;
			}
		
			// get the result
			ParamBag p = call.getResults(answer);
			
			boolean isPN = ((Boolean) p.getValue(0)).booleanValue();
			
			endTime = System.currentTimeMillis();			
			
			System.out.println("-----------------------------------------");
			if(isPN) 	{ System.out.println(number + " is a prime number!"); }	
			else 		{ System.out.println(number + " is not a prime number ...");	}			
			System.out.println("-----------------------------------------");
			System.out.print("Processing Time: ");
			System.out.println(processTime - startTime);
			System.out.print("Total time:      ");
			System.out.println(endTime - startTime);
					
		}
		catch(Exception e) {
			System.out.println("Exception: " + e);
			e.printStackTrace();
		}	
	}
	
	public static void main(String[] args) 
	{
		if(args.length < 1) {
			System.out.println("samples.online.isPrimeNumberClient <number> [proxy server 'IP:port']");
			System.exit(1);
		}
		
		URL	proxy = null;
		if(args.length > 1) { 
			try {
				int sep 		= args[1].indexOf(":");		
				String server 	= args[1].substring(0, sep);
				int port 		= Integer.parseInt(args[1].substring(sep+1));;
				proxy 			= new URL("http", server, port, "/");
			}	
			catch(Exception e) {
				System.out.println("Invalid proxy server.");
				System.exit(1);
			}
		} 
		
		try {
			new isPrimeNumberClient(Integer.parseInt(args[0]), proxy);
		}
		catch(Exception e) {
			System.out.println("'" + args[0] + "' is not an integer!");
		}
	}		
}